﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFrom2
{
    public partial class date : Form
    {
        public date()
        {
            InitializeComponent();
        }

        private void date_Load(object sender, EventArgs e)
        {
            string sql = "select * from Articles";
            var db = Dbhelper.GetDataTable(sql);
            dataGridView1.DataSource = db;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToDeleteRows = false;
        }
        //查找//
        private void button1_Click(object sender, EventArgs e)
        {
            var Author = button1.Text;
            var sql = string.Format("select * from Articles where Author like '%{0}%'", Author);
            var db = Dbhelper.GetDataTable(sql);
            dataGridView1.DataSource = db;
        }
        //添加//
        private void button4_Click(object sender, EventArgs e)
        {
            Add add = new Add();
            var rp = add.ShowDialog();
            if (rp.Equals (DialogResult .OK ))
            {
                var Title = button1.Text;
                string sql = string.Format("select * from Articles where Author like '%{0}%'", Title);
                var db = Dbhelper.GetDataTable(sql);
                dataGridView1.DataSource= db;
            }
        }
        //更改//
        private void button3_Click(object sender, EventArgs e)
        {
            Update update = new Update();
            update.Show();
        }
        //删除//
        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1 .SelectedRows .Count >0)
            {
               var row = dataGridView1.SelectedRows[0];
               var Title = (int)row.Cells["Title"].Value;
               var res = MessageBox.Show("确定删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (res.Equals (DialogResult .Yes ))
                {
                    var sever = string.Format("delete from Articles where Title='{0}' ", Title);
                    var cont = Dbhelper.AddOrEdiOrdeletetSave(sever);
                    if (cont >0)
                    {
                        var name = button2.Text;
                        string sql = string.Format("select * from Articles where Title like '%{0}%'", name);
                        var bt = Dbhelper.GetDataTable(sql);
                        MessageBox.Show("删除成功！");

                    }
                 }else
                {
                    MessageBox.Show("取消成功", "提示");
                }
            }
            
        }
    }
}
